package de.tabuma.sso.user;

import de.tabuma.sso.useruserrelation.UserUserRelationFacade;
import de.tabuma.sso.usergroup.*;
import de.tabuma.sso.logik.*;

import de.tabuma.sso.useruserrelation.UserUserRelation;
import de.tabuma.sso.useruserrelation.*;
import java.util.*;
import de.tabuma.lib.contentmanagement.FacadeInterface;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1 $
 * @since $Date: 2010/01/08 10:03:24 $
 */
public class UserFacade implements FacadeInterface {

    public static int create(User user, String username) {
        int id = (int) PMF.doCreate(user, username);
        UserUserRelation uur = new UserUserRelation();
        uur.setUser(username);
        uur.setUserId(id);
        UserUserRelationFacade.create(uur, username);
        return id;
    }

    public int createElement(Object object, String username) {
        User user = (User) object;
        return create(user, username);
    }

    public static void update(User user, String username) {
        PMF.doUpdate(user, username);
    }

    public void updateElement(Object object, String username) {
        User user = (User) object;
        update(user, username);
    }

    public static User get(int id) {
        return PMF.get(id, new User());
    }

    public Object getElement(int id) {
        return get(id);
    }

    public static void remove(int id, String username) {
        User user = get(id);
        PMF.doRemove(user, username);
        List<UserGroup> ugList = UserGroupFacade.getList1(id);
        for(int i=0;i<ugList.size();i++) {
            UserGroupFacade.remove(ugList.get(id).getId().intValue(), username);
        }
        List<UserUserRelation> uurList = UserUserRelationFacade.getList(id);
        for(int i=0;i<uurList.size();i++) {
            UserUserRelationFacade.remove(uurList.get(i).getId().intValue(), username);
        }
    }

    public void removeElement(int id, String user) {
        remove(id, user);
    }

    public static List<User> getList(int groupId) {
        List<UserGroup> ugList = UserGroupFacade.getList(groupId);
        List<User> uList = new ArrayList<User>();
        for(int i=0;i<uList.size();i++) {
            uList.add(get(ugList.get(i).getUserId()));
        }
        return uList;
    }

    public List getElementList(String listParameter) {
        return getList1(listParameter);
    }
    
    public static List<User> getList1(String user) {
        List<UserUserRelation> uurList = UserUserRelationFacade.getList1(user);
        List<User> uList = new ArrayList<User>();
        for(int i=0;i<uurList.size();i++) {
            UserUserRelation uur = uurList.get(i);
            uList.add(get(uur.getUserId()));
        }
        return uList;
    }
}